<?php
/**
* 名称：排序控制器。
* 描述：排序的控制响应程式。
*/
class Controller_Public_Move extends Controller_Public_Account{
    /**
    * 描述：上移响应程式。
    * @access protected
    * @return void
    */
    protected function MoveUp(){
        $arguments=array(
            'id'=>Route::GetArguments('id'),
            'orderNumber'=>Route::GetArguments('orderNumber')
        );
        //~
        $largerRow=self::$Database->GetRow(
            'SELECT id,orderNumber FROM '.self::$Database->Table($this->MoveSetup['table']).' WHERE orderNumber>? ORDER BY orderNumber ASC ',
            array($arguments['orderNumber'])
        );
        //~
        if($largerRow){
            self::$Database->BeginTransaction();
            $isUpdate=self::$Database->ExecParameterized(
                'UPDATE '.self::$Database->Table($this->MoveSetup['table']).' SET orderNumber=? WHERE id=?',
                array($largerRow['orderNumber'],$arguments['id'])
            );
            if($isUpdate){
                $isUpdate=self::$Database->ExecParameterized(
                    'UPDATE '.self::$Database->Table($this->MoveSetup['table']).' SET orderNumber=? WHERE id=?',
                    array($arguments['orderNumber'],$largerRow['id'])
                );
            }
            if($isUpdate){
                self::$Database->Commit();
                header('location:'.$_SERVER['HTTP_REFERER']);
            }
            else{
                self::$Database->Rollback();
                Model_Break::Message('操作失败，请稍候再试！');
            }
        }
        else{
            header('location:'.$_SERVER['HTTP_REFERER']);
        }
    }

    /**
    * 描述：下移响应程式。
    * @access protected
    * @return void
    */
    protected function MoveDown(){
        $arguments=array(
            'id'=>Route::GetArguments('id'),
            'orderNumber'=>Route::GetArguments('orderNumber')
        );
        //~
        $smallerRow=self::$Database->GetRow(
            'SELECT id,orderNumber FROM '.self::$Database->Table($this->MoveSetup['table']).' WHERE orderNumber<? ORDER BY orderNumber DESC ',
            array($arguments['orderNumber'])
        );
        //~
        if($smallerRow){
            self::$Database->BeginTransaction();
            $isUpdate=self::$Database->ExecParameterized(
                'UPDATE '.self::$Database->Table($this->MoveSetup['table']).' SET orderNumber=? WHERE id=?',
                array($smallerRow['orderNumber'],$arguments['id'])
            );
            if($isUpdate){
                $isUpdate=self::$Database->ExecParameterized(
                    'UPDATE '.self::$Database->Table($this->MoveSetup['table']).' SET orderNumber=? WHERE id=?',
                    array($arguments['orderNumber'],$smallerRow['id'])
                );
            }
            if($isUpdate){
                self::$Database->Commit();
                header('location:'.$_SERVER['HTTP_REFERER']);
            }
            else{
                self::$Database->Rollback();
                Model_Break::Message('操作失败，请稍候再试！');
            }
        }
        else{
            header('location:'.$_SERVER['HTTP_REFERER']);
        }
    }
}